<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends Controller 
{
	public function lst()
	{
		$serch=I('post.');
        $model = D('Article');
        $data = $model->search();
        $site = D('Site')->site_list();

        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
            'count' => $data['count'],
            'site' => $site,
        ));
        $this->display();
	}

	public function add()
    {
    	if(IS_POST)
        {
            set_time_limit(0);
            $model = D('Admin/Article');
            if($model->create(I('post.'), 1))
            {
                if($id = $model->add())
                {
                    $this->success('添加成功！', U('lst?p='.I('get.p')));
                    exit;
                }
            }
            $this->error($model->getError());
        }

        // 取出所有的文章分类
        $catModel = D('ArticlesCategory');
        $catData = $catModel->getTree();
        $this->assign('catData', $catData);

        // 取出所有站点
        $site = D('Site')->site_list();
        $this->assign('site', $site);

        //栏目列表页id
        $cat_id = I('get.cat_id');
        if(!empty($cat_id)){
            $this->assign('cat_id',$cat_id);
        }

        $parentModel = D('Admin/ArticlesCategory');
        $parentData = $parentModel->getTree();
        $children = $parentModel->getChildren($id);
        $this->assign(array(
            'parentData' => $parentData,
            'children' => $children,
        ));

        // $this->setPageBtn('添加文章', '文章列表', U('lst?p='.I('get.p')));
        $this->display();
    }

    public function edit()
    {
    	$model = D('Admin/Article');
    	$id = I('get.id', 0, 'intval');
    	if(IS_POST)
    	{
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst', array('p' => I('get.p', 1))));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
        
        // 取出所有站点
        $site = D('Site')->site_list();
        $this->assign('site', $site);

        $parentModel = D('Admin/ArticlesCategory');
        $parentData = $parentModel->getTree();
        $children = $parentModel->getChildren($id);
        $this->assign(array(
            'parentData' => $parentData,
            'children' => $children,
        ));

    	$data = $model->find($id);
        $this->assign('data', $data);
		// $this->setPageBtn('修改预约', '预约列表', U('lst?p='.I('get.p')));
		$this->display();
    }

	public function del(){
		$ids = I('ids');
		if(is_array($ids)){
			foreach ($ids as $key => $value) {
				$ids[$key] = (int)$value;
			}
			$where = array('id'=>array('in', $ids));
		}else{
			$ids = intval($ids);
			$where = array('id'=>array('eq', $ids));
		}
		
		if(M('Article')->where($where)->delete()){
			$result['code'] = 0;
		}
		$this->ajaxReturn($result);
	}
}














